<?php
$aboutFileUrlList = [];
$repoUrl = getRepoUrl();
$repoUrl = str_replace('.git', '', $repoUrl);

// 检查有哪些文件有更新
$cmd = "git status";
exec($cmd, $output, $statusCode);
echo $output = implode(PHP_EOL, $output);
// 匹配新增目录
// 2020/03/13/
$pattern = '@(\d{4,}/\d\d/\d\d/)@';
preg_match_all($pattern, $output, $matchRes);
if (!empty($matchRes[1])) {
    foreach($matchRes[1] as $k=>$v) {
        $cmd = "git add {$v}";
        exec($cmd, $output2, $statusCode);
    }
}
// 新文件
$statusString = getGitStatus();
$pattern = '@新文件：\s+(\d+/\d\d/\d\d/\w+\.json)@';
preg_match_all($pattern, $output, $matchRes);
if (!empty($matchRes[1])) {
    foreach($matchRes[1] as $k=>$v) {
        $cmd = "git add {$v}";
        exec($cmd, $output2, $statusCode);
        $fileUrl = "{$repoUrl}/raw/master/{$v}";
        $aboutFileUrlList[] = $fileUrl;
    }
}
$output = getGitStatus();
// create mode 100644 2020/03/16/multi_portrait.json
$pattern = '@create mode \d+ \s+(\d+/\d\d/\d\d/\w+\.json)@';
preg_match_all($pattern, $output, $matchRes);
if (!empty($matchRes[1])) {
    foreach($matchRes[1] as $k=>$v) {
        $cmd = "git add {$v}";
        exec($cmd, $output2, $statusCode);
        $fileUrl = "{$repoUrl}/raw/master/{$v}";
        $aboutFileUrlList[] = $fileUrl;
    }
}
$pattern = '@新文件：\s+(\d+/\d\d/\d\d/\w+\.json)@';
preg_match_all($pattern, $output, $matchRes);
if (!empty($matchRes[1])) {
    foreach($matchRes[1] as $k=>$v) {
        $cmd = "git add {$v}";
        exec($cmd, $output2, $statusCode);
        $fileUrl = "{$repoUrl}/raw/master/{$v}";
        $aboutFileUrlList[] = $fileUrl;
    }
}
// 修改文件
$output = getGitStatus();
$pattern = '@修改：\s+([^\n]+)\s*\n@';
preg_match_all($pattern, $output, $matchRes);
echo PHP_EOL;
if (!empty($matchRes[1])) {
    foreach($matchRes[1] as $k=>$v) {
        $fileUrl = "{$repoUrl}/raw/master/{$v}";
        $aboutFileUrlList[] = $fileUrl;
    }
}
// 新增目录跟踪处理
/**
 * 未跟踪的文件:
  （使用 "git add <文件>..." 以包含要提交的内容）
        2020/03/12/


    （使用 "git restore --staged <文件>..." 以取消暂存）
    新文件：   2020/03/12/portrait_v2.json
 * 
 * 
 */
// todo

echo "\n--------------\n";
$cmd = "git add .";
exec($cmd, $output, $statusCode);
echo $output = implode(PHP_EOL, $output);
echo "\n--------------\n";

$cmd = "git commit -m \"update json content \"";
exec($cmd, $output, $statusCode);
echo $output = implode(PHP_EOL, $output);
// 如果是新创建的文件，则在此时打印出记录
// create mode 100644 2020/03/10/progress_info.json
$pattern = '@create mode 100644\s+([^\n]+)\s*\n@';
preg_match_all($pattern, $output, $matchRes);
if (!empty($matchRes[1])) {
    foreach($matchRes[1] as $k=>$v) {
        $fileUrl = "{$repoUrl}/raw/master/{$v}";
        $aboutFileUrlList[] = $fileUrl;
    }
}
echo "\n--------------\n";

$cmd = "git push";
exec($cmd, $output, $statusCode);
echo $output = implode(PHP_EOL, $output);

// 输出相关的文件地址
echo "\n---------相关的文件列表---------\n";
$output = implode(PHP_EOL, $aboutFileUrlList);
fwrite(STDOUT, $output);
echo "\n---------end---------\n";


//-------------------一些函数---------------------------------------------
function info($msg = '')
{
    $datetime = date('Y-m-d H:i:s');
    $content = "{$datetime}\t$msg\n";

    echo $content;
}

// 获取当前仓库地址
function getRepoUrl()
{
    $cmd = 'git remote get-url origin';
    exec($cmd, $output, $code);
    $url = $output[0] ?? '';
    if (empty($url)) throw new \Exception("找不到仓库地址", -1);
    return $url;
}

function getGitStatus()
{
    $cmd = "git status";
    exec($cmd, $output, $statusCode);
    $output = implode(PHP_EOL, $output);

    return $output;
}
